#! /usr/bin/python3
# author ckm

from collections.abc import Iterable
class MyList:
    def __init__(self):
        self.container=[]

    def add(self,item):
        self.container.append(item)

    # 只要重写了iter方法，就会变为可迭代的
    def __iter__(self):
        myiterator = MyIterator(self)
        return myiterator
class MyIterator:
    def __init__(self,MyList):
        self.mylist:MyList = MyList
        self.current = 0
    def __iter__(self):
        return self
    def __next__(self):
        current = self.current
        self.current += 1
        if current < len(self.mylist.container):
            return self.mylist.container[current]
        else: raise StopIteration

if __name__ == '__main__':
    mylist=MyList()
    mylist.add(1)
    mylist.add(2)
    mylist.add(3)
    print(isinstance(mylist,Iterable))
    # for i in mylist:
    #     print(i)
    for i in mylist:
        print(i)
    myiter = MyIterator(mylist)
    print(next(myiter))
    print(next(myiter))
    print(next(myiter))
    print(next(myiter))


